package net.zhengxinyun.performance.controller.indicators;


import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.dto.TownDTO;
import net.zhengxinyun.performance.entity.TokenEntity;
import net.zhengxinyun.performance.entity.TownEntity;
import net.zhengxinyun.performance.entity.TownIndicatorsEntity;
import net.zhengxinyun.performance.filter.TownFilter;
import net.zhengxinyun.performance.filter.TownIndicatorsFilter;
import net.zhengxinyun.performance.service.indicators.TownIndicatorsService;
import net.zhengxinyun.performance.service.indicators.TownScoreService;
import net.zhengxinyun.performance.service.login.TokenService;
import net.zhengxinyun.performance.service.indicators.TownService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: hyp
 * @Date: 2018/10/29 20:28
 */
@RestController
@RequestMapping("/townIndicators")
public class TownIndicatorsController {

    private TownIndicatorsService townIndicatorsService;
    private TownService townService;
    private TokenService tokenService;
    private TownScoreService townScoreService;

    @Autowired
    public TownIndicatorsController(TownIndicatorsService townIndicatorsService, TownService townService, TokenService tokenService, TownScoreService townScoreService) {
        this.townIndicatorsService = townIndicatorsService;
        this.townService = townService;
        this.tokenService = tokenService;
        this.townScoreService = townScoreService;
    }

    @PostMapping("/insert")
    public Result<String> insert(@RequestBody TownIndicatorsParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            if (param.list == null){
                String str = townIndicatorsService.insert(param.townIndicatorsEntity,param.townList);
                if (!"添加成功".equals(str)){
                    return ResultUtils.getResults(Result.Status.ERROR,str);
                }else {
                    return ResultUtils.getSuccessResults();
                }
            }else {
                String str = townIndicatorsService.ListInsert(param.list);
                if (!"添加成功".equals(str)){
                    return ResultUtils.getResults(Result.Status.ERROR,str);
                }else {
                    return ResultUtils.getSuccessResults();
                }
            }

        }
        return ResultUtils.getResults(Result.Status.OVERTIME);
    }

    @PostMapping("/delete")
    public Result<String> delete(@RequestBody TownIndicatorsParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            String str = townIndicatorsService.delete(param.townIndicatorsEntity);
            if (!"删除成功".equals(str)){
                return ResultUtils.getResults(Result.Status.ERROR,str);
            }
            else {
                return ResultUtils.getSuccessResults();
            }
        }

        return ResultUtils.getResults(Result.Status.OVERTIME);
    }

    @PostMapping("/update")
    public Result<String> update(@RequestBody TownIndicatorsParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
           String str = townIndicatorsService.update(param.townIndicatorsEntity,param.townList);
           if (!"更新成功".equals(str)){
               return ResultUtils.getResults(Result.Status.ERROR,str);
           }
           return ResultUtils.getSuccessResults();
        }
        return ResultUtils.getResults(Result.Status.OVERTIME);
    }

    @PostMapping("/query")
    public Result<HashMap<String,Object>> query(@RequestBody TownIndicatorsParam param){
        if (tokenService.compareToken(param.tokenEntity.getValue())){
            if (param.townIndicatorsFilter.getPageSize() == 0){
                param.townIndicatorsFilter.setPageSize(10);
            }
            HashMap<String,Object> map = new HashMap<>();
            TownIndicatorsFilter filter = new TownIndicatorsFilter();
            filter.setIndexType("一级指标");
            List<TownIndicatorsFilter> firstIndexList = townIndicatorsService.query(filter);
            map.put("firstIndex",firstIndexList);//给一级指标集合
            Double pageTotal = 0.0;
            if ("一级指标".equals(param.townIndicatorsFilter.getIndexType())){
                List<TownIndicatorsFilter>  list =  townIndicatorsService.query(param.townIndicatorsFilter);
                if (list.size()==0){
                    param.townIndicatorsFilter.setPageNumber(param.townIndicatorsFilter.getPageNumber()-1);
                    list = townIndicatorsService.query(param.townIndicatorsFilter);
                }
                map.put("list",list);
                pageTotal = Util.intGetTotalPage(townIndicatorsService.queryForCount(param.townIndicatorsFilter),param.townIndicatorsFilter.getPageSize());
            }else if ("二级指标".equals(param.townIndicatorsFilter.getIndexType())){
                List<Map<String,Object>> secList = townIndicatorsService.querySec(param.townIndicatorsFilter);
                if (secList.size() == 0){
                    param.townIndicatorsFilter.setPageNumber(param.townIndicatorsFilter.getPageNumber()-1);
                    secList = townIndicatorsService.querySec(param.townIndicatorsFilter);
                }
                map.put("list",secList);
                pageTotal = Util.intGetTotalPage(townIndicatorsService.queryForCount(param.townIndicatorsFilter),param.townIndicatorsFilter.getPageSize());
            }
            map.put("pageTotal",pageTotal);
            return ResultUtils.getSuccessResults(map);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME,"token不对",null);
    }
}

class TownIndicatorsParam{
    public TownIndicatorsFilter townIndicatorsFilter;
    public TownIndicatorsEntity townIndicatorsEntity;
    public TokenEntity tokenEntity;
    public List<TownEntity> townList;
    public TownEntity townEntity;
    public TownFilter townFilter;
    public List<TownDTO> list;
    public TownDTO townDTO;
}
